package utils;

import book.Book;
import com.bit.utils.FileUtils;
import constant.Constant;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class AnalyzingBook {

    //给文件写数据
    public void storeObject(Book[] books, String filename) {
        //1.需要先计算 有多少本有效书籍
        int booksUseLen = 0;
        for (int i = 0; i < books.length; i++) {
            if(books[i] != null) {
                booksUseLen++;
            }
        }

        StringBuilder jsonArray = new StringBuilder();
        for (int i = 0; i < booksUseLen; i++) {
            if(books[i] != null) {
                jsonArray.append(books[i].toJSON());
            }
            if (i != booksUseLen-1) {
                //一本书籍完成后以\n进行分割
                jsonArray.append("\n");
            }
        }

        FileUtils.writeFile(jsonArray.toString(),filename);
    }


    //给文件读数据
    public Book[] loadObject(String filename) {
        //从文件读取数据
        //1. 从文件读取数据
        String content = FileUtils.readFile(filename);
        if (content == null || content.isEmpty()) {
            System.out.println("File is empty or does not exist: " + filename);
            return null;
        }
        //2. 使用\n作为分隔符进行字符串分割
        String[] bookJsonStrings = content.split("\n");

        Book[] books = new Book[bookJsonStrings.length];

        for (int i = 0; i < bookJsonStrings.length; i++) {
            books[i] = parseBookJson(bookJsonStrings[i]);
        }

        return books;
    }

    //把读出来的数据进行解析
    private Book parseBookJson(String json) {
        if(json.isEmpty()) {
            return null;
        }

        String[] pairs = json.split(",");

        int bookId = Integer.parseInt(pairs[0]);
        String title = pairs[1];
        String author = pairs[2];
        String category = pairs[3];
        int publishYear = Integer.parseInt(pairs[4]);
        boolean isBorrowed = Boolean.parseBoolean(pairs[5]);
        int borrowCount = Integer.parseInt(pairs[6]);
        //DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDate shelfDate = LocalDate.parse(pairs[7]);

        //4. 构造书籍对象
        if (title != null && author != null && category != null && shelfDate != null) {
            Book book = new Book(title, author, category, publishYear, shelfDate);
            book.setBorrowed(isBorrowed);
            book.setBorrowCount(borrowCount);
            book.setBookId(bookId);
            return book;
        }

        return null;
    }


    public static void main(String[] args) {
        AnalyzingBook analyzingBook = new AnalyzingBook();
        Book[] ret = analyzingBook.loadObject(Constant.ALL_BOOK_FILE_NAME);
        for (int i = 0; i < ret.length; i++) {
            System.out.println(ret[i]);
        }
    }

    public static void main1(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book("java", "zhangsan", "编程", 1994, LocalDate.of(2023, 9, 24));
        books[1] = new Book("mysql", "lisi", "编程", 1999, LocalDate.of(2024, 2, 10));
        books[2] = new Book("php", "wangwu", "编程", 2020, LocalDate.of(2023, 9, 23));
        books[3] = new Book("西游记", "吴承恩", "小说", 2024, LocalDate.of(2023, 9, 23));

        AnalyzingBook analyzingBook = new AnalyzingBook();
        analyzingBook.storeObject(books,Constant.ALL_BOOK_FILE_NAME);

    }
}
